import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Stack;

public class Solution {
    //将小数转换为2进制
    public String printBin(double num) {
        StringBuilder s = new StringBuilder("0.");
        double ret = num * 2;
        for (int i = 0; i < 6; i++) {
            if (ret > 1) {
                s.append('1');
                ret = (ret - 1) * 2;
            } else {
                s.append('0');
                ret = ret * 2;
            }
        }
        String s1 = new String(s);
        if (ret == 0) {
            return s1;
        }
        return "ERROR";
    }

    //变味词组
    public List<List<String>> groupAnagrams(String[] strs) {
        Stack<String> stack1 = new Stack<>();
        Stack<String> stack2 = new Stack<>();
        List<List<String>> grouplist = new ArrayList<>();
        //全部入栈
        for (int i = 0; i < strs.length; i++) {
            stack1.push(strs[i]);
        }
        int m = strs[0].length();
        //拿出元素和栈内元素比较，相等放入链表中，不相等放入栈2中,直到栈空
        while ((!stack1.isEmpty()&&stack2.isEmpty()) || (!stack2.isEmpty()&&stack1.isEmpty())) {
            if (!stack1.isEmpty()) {
                List list = new ArrayList();
                String ret = stack1.pop();
                //先将第一个元素放入链表中
                list.add(ret);
                //判断栈顶元素是否和拿到的元素相等，相等就放入链表中,不相等放入栈2中
                //这里的相等是计算值相等
                while (!stack1.isEmpty()) {
                    if (comper(ret,stack1.peek())) {
                        list.add(stack1.pop());
                    } else {
                        stack2.push(stack1.pop());
                    }
                }
                grouplist.add(list);
            }
            if(!stack2.isEmpty()) {
                List list = new ArrayList();
                String ret = stack2.pop();
                //先将第一个元素放入链表中
                list.add(ret);
                //判断栈顶元素是否和拿到的元素相等，相等就放入链表中,不相等放入栈1中
                //这里的相等是计算值相等
                while (!stack2.isEmpty()) {
                    if(comper(ret,stack2.peek())) {
                        list.add(stack2.pop());
                    }else {
                        stack1.push(stack2.pop());
                    }
                }
                grouplist.add(list);
            }
        }
        return grouplist;
    }
    public boolean comper(String s1,String s2) {
        if(s1.equals("") && !s2.equals("")) {
            return false;
        }
        if(!s1.equals("") && s2.equals("")) {
            return false;
        }
        if(s1.equals("") &&s2.equals("")) {
            return true;
        }
        char[] a = s1.toCharArray();
        char[] b = s2.toCharArray();
        Arrays.sort(a);
        Arrays.sort(b);
        if(a.length > b.length || a.length < b.length) {
            return false;
        }
        for (int i = 0; i < a.length; i++) {
            if(a[i] != b[i]) {
                return false;
            }
        }
        return true;
    }
}
